package swing.frame;

import client.UserSession;
import swing.BaseFrame;
import swing.FrameManager;
import utils.CommonUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ProfileFrame extends BaseFrame {

    public final static String frameTitle = "个人主页";

    public JPanel profilePanel;

    //子版块
    public JPanel achievementPanel; //成就版块
    public JPanel desktopWordPanel; //桌面单词设置版块

    public JButton userPortraitLabel;
    public JLabel userAccountLabel;
    public JLabel exitLoginLabel;

    //成就版块组件
    public JLabel wordContributeLabel;
    public JLabel meaningContributeLabel;
    public JLabel mUpdateContributeLabel;

    //桌面单词设置版块组件
    public JLabel desktopWordSwitch;
    public boolean switchState; //桌面单词开关状态
    public ImageIcon offIcon; //关图标
    public JTextField refreshTimeField;

    @Override
    protected void initFrame() {
        this.setTitle(frameTitle);
        profilePanel = new JPanel();
        profilePanel.setLayout(null);
        Color color = new Color(230, 240, 255);
        profilePanel.setBackground(color);
        this.setContentPane(profilePanel);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setPreferredSize(new Dimension(300, 600));
        this.setResizable(false);
        this.pack();
    }

    @Override
    protected void initAllComponent() {
        //用户头像
        userPortraitLabel = new JButton();
        userPortraitLabel.setBounds(12, 12, 56,56);
        userPortraitLabel.setFocusPainted(false);
        ImageIcon userIcon = new ImageIcon(CommonUtils.getFileUrl("img/default_portrait.png"));
        userIcon.setImage(userIcon.getImage().getScaledInstance(50, 50,Image.SCALE_DEFAULT ));
        userPortraitLabel.setIcon(userIcon);
        //用户名称
        userAccountLabel = new JLabel();
        userAccountLabel.setBounds(80, 25,200, 56);
        String account = UserSession.UserAccount().isEmpty() ? "未登录" : UserSession.UserAccount();
        userAccountLabel.setText(account);
        userAccountLabel.setFont(new Font("楷体", Font.PLAIN, 25));
        //用户成就
        achievementPanel = new JPanel();
        achievementPanel.setLayout(null);
        achievementPanel.setBounds(12, 80, 220, 180);
        initAchieveComp();
        //桌面单词设置
        desktopWordPanel = new JPanel();
        desktopWordPanel.setLayout(null);
        desktopWordPanel.setBounds(12, 357, 220, 90);
        initDesktopWordComp();
        //退出登录按钮
        exitLoginLabel = new JLabel();
        exitLoginLabel.setBounds(15, 455, 80, 60);
        exitLoginLabel.setText("< 退出登录");
        exitLoginLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        exitLoginLabel.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                UserSession.clearLoginSession();
                FrameManager.closeProfileFrame();
                FrameManager.hideMainFrame();
                FrameManager.showLoginFrame();
                FrameManager.hideDesktopWordFrame();
            }
            @Override
            public void mousePressed(MouseEvent e) {
            }
            @Override
            public void mouseReleased(MouseEvent e) {
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                Color wordTextColor = new Color(30, 144, 255);
                exitLoginLabel.setForeground(wordTextColor);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                exitLoginLabel.setForeground(Color.BLACK);
            }
        });
    }

    @Override
    protected void addAllComponent() {
        profilePanel.add(userPortraitLabel);
        profilePanel.add(userAccountLabel);
        profilePanel.add(achievementPanel);
        profilePanel.add(desktopWordPanel);
        profilePanel.add(exitLoginLabel);
    }

    private void initAchieveComp() {
        JLabel achieveLabel = new JLabel("个人成就");
        achieveLabel.setBounds(3, 5, 100, 30);
        achieveLabel.setFont(new Font("宋体", Font.BOLD, 16));
        achievementPanel.add(achieveLabel);
        //单词贡献图标
        JLabel cwImageLabel = new JLabel();
        cwImageLabel.setBounds(0, 40, 40, 40);
        ImageIcon cwIcon = new ImageIcon(CommonUtils.getFileUrl("img/contribute_word.png"));
        cwIcon.setImage(cwIcon.getImage().getScaledInstance(40, 40,Image.SCALE_DEFAULT ));
        cwImageLabel.setIcon(cwIcon);
        achievementPanel.add(cwImageLabel);
        //单词贡献
        wordContributeLabel = new JLabel();
        wordContributeLabel.setBounds(50, 40, 150, 40);
        Font cFont = new Font("宋体", Font.PLAIN, 14);
        wordContributeLabel.setText("贡献了单词 " + UserSession.user.getWord_count() + " 个");
        wordContributeLabel.setFont(cFont);
        achievementPanel.add(wordContributeLabel);
        //单词释义贡献图标
        JLabel cmImageLabel = new JLabel();
        cmImageLabel.setBounds(3, 85, 30, 30);
        ImageIcon cmIcon = new ImageIcon(CommonUtils.getFileUrl("img/contribute_meaning.png"));
        cmIcon.setImage(cmIcon.getImage().getScaledInstance(30, 30,Image.SCALE_DEFAULT ));
        cmImageLabel.setIcon(cmIcon);
        achievementPanel.add(cmImageLabel);
        //单词释义贡献
        meaningContributeLabel = new JLabel();
        meaningContributeLabel.setBounds(50, 83, 150, 40);
        meaningContributeLabel.setText("贡献了单词释义 " + UserSession.user.getMeaning_count() + " 个");
        meaningContributeLabel.setFont(cFont);
        achievementPanel.add(meaningContributeLabel);
        //单词释义修正贡献图标
        JLabel cmuImageLabel = new JLabel();
        cmuImageLabel.setBounds(2, 124, 35, 35);
        ImageIcon cmuIcon = new ImageIcon(CommonUtils.getFileUrl("img/contribute_meaning_update.png"));
        cmuIcon.setImage(cmuIcon.getImage().getScaledInstance(33, 33,Image.SCALE_DEFAULT ));
        cmuImageLabel.setIcon(cmuIcon);
        achievementPanel.add(cmuImageLabel);
        //单词释义修正贡献
        mUpdateContributeLabel = new JLabel();
        mUpdateContributeLabel.setBounds(50, 124, 150, 40);
        mUpdateContributeLabel.setText("修正了单词释义 " + UserSession.user.getUpdate_meaning_count() + " 次");
        mUpdateContributeLabel.setFont(cFont);
        achievementPanel.add(mUpdateContributeLabel);
    }

    private void initDesktopWordComp() {
//        JLabel staticText0 = new JLabel("Settings");
//        staticText0.setBounds(5,0, 100, 45);
//        staticText0.setFont(new Font("宋体", Font.BOLD, 15));
//        desktopWordPanel.add(staticText0);
        int benchmarkY = 5;
        JLabel staticText1 = new JLabel("桌面单词");
        staticText1.setBounds(5,benchmarkY, 100, 45);
        Font font = new Font("宋体", Font.BOLD, 15);
        staticText1.setFont(font);
        desktopWordPanel.add(staticText1);
        desktopWordSwitch = new JLabel();
        desktopWordSwitch.setBounds(158, 2+benchmarkY, 50, 40);
        offIcon = new ImageIcon(CommonUtils.getFileUrl("img/turn_off.png"));
        offIcon.setImage(offIcon.getImage().getScaledInstance(36, 32,Image.SCALE_DEFAULT ));
        desktopWordSwitch.setIcon(offIcon);
        ImageIcon onIcon = new ImageIcon(CommonUtils.getFileUrl("img/turn_on.png"));
        onIcon.setImage(onIcon.getImage().getScaledInstance(36, 32,Image.SCALE_DEFAULT ));
        desktopWordSwitch.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                switchState = !switchState;
                if (switchState) {
                    desktopWordSwitch.setIcon(onIcon);
                    FrameManager.showDesktopWordFrame(Integer.valueOf(refreshTimeField.getText().trim())*1000);
                } else {
                    desktopWordSwitch.setIcon(offIcon);
                    FrameManager.hideDesktopWordFrame();
                }
            }
        });
        desktopWordPanel.add(desktopWordSwitch);
        JLabel staticText2 = new JLabel("单词刷新时间");
        staticText2.setBounds(5,35+benchmarkY, 100, 45);
        staticText2.setFont(font);
        desktopWordPanel.add(staticText2);
        refreshTimeField = new JTextField("120");
        refreshTimeField.setBounds(160, 48+benchmarkY, 40, 20);
        refreshTimeField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                if(e.getKeyChar()==KeyEvent.VK_ENTER) {
                    int refreshSeconds = Integer.valueOf(refreshTimeField.getText().trim());
                    if (refreshSeconds < DesktopWordFrame.MIN_REFRESH_TIME) {
                        refreshSeconds = DesktopWordFrame.MIN_REFRESH_TIME;
                        showMessageDialog("单词刷新时间最少为" + DesktopWordFrame.MIN_REFRESH_TIME + "秒");
                    } else if (refreshSeconds > DesktopWordFrame.MAX_REFRESH_TIME) {
                        refreshSeconds = DesktopWordFrame.MAX_REFRESH_TIME;
                        showMessageDialog("单词刷新时间最多为" + DesktopWordFrame.MAX_REFRESH_TIME + "秒");
                    }
                    refreshTimeField.setText(refreshSeconds + "");
                    FrameManager.changeWordRefreshTime(refreshSeconds*1000);
                    desktopWordPanel.requestFocus();
                }
            }
        });
        desktopWordPanel.add(refreshTimeField);
        JLabel staticText3 = new JLabel("s");
        staticText3.setBounds(202,50+benchmarkY, 10, 20);
        staticText3.setFont(font);
        desktopWordPanel.add(staticText3);
    }

    public void flushAchievement() {
        wordContributeLabel.setText("贡献了单词 " + UserSession.user.getWord_count() + " 个");
        meaningContributeLabel.setText("贡献了单词释义 " + UserSession.user.getMeaning_count() + " 个");
        mUpdateContributeLabel.setText("修正了单词释义 " + UserSession.user.getUpdate_meaning_count() + " 次");
    }

    public void offDesktopWordSwitch() {
        switchState = false;
        desktopWordSwitch.setIcon(offIcon);
    }
}
